package com.itlxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itlxd.entity.AddressBook;
import com.itlxd.global.SysConstants;
import com.itlxd.mapper.AddressBookMapper;
import com.itlxd.service.IAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 地址管理 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Transactional
@Service
public class AddressBookServiceImpl implements IAddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> listAddressesByUserId(Long userId) {
        LambdaQueryWrapper<AddressBook> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != userId, AddressBook::getUserId, userId);
        return addressBookMapper.selectList(lambdaQuery);
    }

    @Override
    public Integer insertAddressBook(AddressBook addressBook) {
        //查询用户是否有地址，没有则设置添加的地址为默认地址
        List<AddressBook> addressBooks = listAddressesByUserId(addressBook.getUserId());
        if (null != addressBooks && addressBooks.size() > 0) {
            addressBook.setIsDefault(SysConstants.NO);
        }else {
            addressBook.setIsDefault(SysConstants.YES);
        }
        return addressBookMapper.insert(addressBook);
    }

    @Override
    public AddressBook getOneById(Long id) {
        return addressBookMapper.selectById(id);
    }

    @Override
    public Integer updateAddressBook(AddressBook addressBook) {
        return addressBookMapper.updateById(addressBook);
    }

    @Override
    public Integer deleteAddressBook(Long[] ids) {
        return addressBookMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer updateDefaultAddressBook(AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(AddressBook::getUserId, addressBook.getUserId());
        List<AddressBook> addressBooks = addressBookMapper.selectList(lambdaQuery);

        if (null != addressBooks) {
            for (AddressBook address : addressBooks) {
                if ((address.getIsDefault() == SysConstants.YES)) {
                    address.setIsDefault(SysConstants.NO);
                    addressBookMapper.updateById(address);
                    break;
                }
            }
        }

        addressBook.setIsDefault(SysConstants.YES);
        return addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBook getDefaultAddressBook(Long userId) {
        LambdaQueryWrapper<AddressBook> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != userId, AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, SysConstants.YES);
        List<AddressBook> addressBooks = addressBookMapper.selectList(lambdaQuery);
        return addressBooks.get(SysConstants.NUM_ZERO);
    }
}
